#coding: UTF-8

import re
import urllib
import urllib2
import sys

n = len(sys.argv);
filename = ''
username = ''

if  n == 3 :
	filename = sys.argv[1]
	username = sys.argv[2]
else:
	quit()

f = open(r'd:\%s.txt'%filename,'w+')
linenumber = 0
def save(f):
	for i in range(len(urlget)):
		global linenumber
		linenumber += 1
		w = "line %s:"%linenumber +" "+description[i]+ "  "+urlget[i]+"\n"
		f.write(w)
		print(w)

	
page = 0

urlsrc = r'http://www.douban.com/people/%s/likes?start=%s'%(username,page)
print(urlsrc)

pattern = re.compile(r'(?<=<h3><a href=).*?(?=">)')
pattern2= re.compile(r'(?<=">).*?(?=</a></h3>)')


while(1):
	content = urllib2.urlopen(urlsrc).read()
	urlget = pattern.findall(content)
	description = pattern2.findall(content)
	print "urlget=%s,title=%s"%(len(urlget),len(description))
	if urlget:
		save(f);
	else:
		print "error or complete"
		break;
	page += 15
	urlsrc = r'http://www.douban.com/people/%s/likes?start=%s'%(username,page)
	
f.close()











